//
//  HMNavigationController.swift
//  HM22
//
//  Created by Apple on 16/11/29.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

class HMNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        //  设置边缘手势代理
        self.interactivePopGestureRecognizer?.delegate = self
        
    }

   
    //  重写push
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        //  判断大于0，tabbar隐藏
        if viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
        }
        
        
        super.pushViewController(viewController, animated: animated)
        //  判断如果控制器个数大于1，处理想要的逻辑
        if viewControllers.count > 1 {
            
            if viewControllers.count == 2 {
                //  获取第一个控制器的标题
                let title = viewControllers.first!.title!
                //  设置标题
                viewController.title = "当前显示的是第\(viewControllers.count)级控制器"
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
                
            } else {
                //  设置标题
                viewController.title = "当前显示的是第\(viewControllers.count)级控制器"
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
            }
            
        }
        
        
        
        
    }
    
    
    // MARK: --点击返回按钮的操作
    @objc private func popAction() {
        popViewController(animated: true)
    }
    

    //  是否开始处理这次的边缘收拾
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        //  根视图不处理边缘手势，防止push的时候卡住
        if viewControllers.count == 1{
            return false
        }
        
        return true
        
    }
    
}
